﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Common;
using MarginTrading.Services;

namespace MarginTrading.Forms
{
    public partial class EditAdvisor : Form
    {
        private bool bEditing = false;
        private string sEditingAdvisor = "";

        public EditAdvisor()
        {
            InitializeComponent();
        }

        public EditAdvisor(string agentCode)
        {
            sEditingAdvisor = agentCode;
            InitializeComponent();
            AcceptButton = buttonOK;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (validateInputs() == false)
            {
                return;
            }

            Advisor advisor = new Advisor();
            advisor.Code = sEditingAdvisor;
            advisor.Name = textBoxAdvisorName.Text;
            advisor.Title = textBoxTitle.Text;
            advisor.Initials = textBoxInitials.Text;
            advisor.Surname = textBoxSurname.Text;
            advisor.Address_1 = textBoxAddress1.Text;
            advisor.Address_2 = textBoxAddress2.Text;
            advisor.Telephone1 = textBoxTelephone.Text;
            advisor.Fax = textBoxFax.Text;

            try
            {
                AdvisorServices.Update(advisor);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Failed to update advisor information\nError: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            MessageBox.Show("Advisor information updated successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Close();
        }

        private bool validateInputs()
        {
            return true;
        }

        private void buttonCancel_Click_1(object sender, EventArgs e)
        {
            Close();
        }

        private void buttonGO_Click(object sender, EventArgs e)
        {
            Advisor advisor = null;
            try
            {
                advisor = AdvisorServices.GetAdvisorByCode(comboBoxAdvisorCode.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Invalid Agent Code", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (advisor == null)
            {
                MessageBox.Show("Invalid Agent Code", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            EnableControls(true);

            textBoxAdvisorName.Text = advisor.Name;
            textBoxTitle.Text = advisor.Title;
            textBoxInitials.Text = advisor.Initials;
            textBoxSurname.Text = advisor.Surname;
            textBoxAddress1.Text = advisor.Address_1;
            textBoxAddress2.Text = advisor.Address_2;
            textBoxTelephone.Text = advisor.Telephone1;
            textBoxFax.Text = advisor.Fax;

            bEditing = true;
            sEditingAdvisor = comboBoxAdvisorCode.Text;
        }

        private void EnableControls(bool bEnable)
        {
            textBoxAdvisorName.Enabled = bEnable;
            textBoxTitle.Enabled = bEnable;
            textBoxInitials.Enabled = bEnable;
            textBoxSurname.Enabled = bEnable;
            textBoxAddress1.Enabled = bEnable;
            textBoxAddress2.Enabled = bEnable;
            textBoxTelephone.Enabled = bEnable;
            textBoxFax.Enabled = bEnable;
            buttonOK.Enabled = bEnable;
        }

        private void EditAdvisor_Load(object sender, EventArgs e)
        {
            if (sEditingAdvisor != "")
            {
                comboBoxAdvisorCode.Items.Add(sEditingAdvisor);
                comboBoxAdvisorCode.SelectedIndex = 0;
                comboBoxAdvisorCode.Enabled = false;
                bEditing = true;
                buttonGO_Click(null, null);
                return;
            }

            try
            {
	            AdvisorCollection advisorCollection = AdvisorServices.GetAdvisorCollection();
	
	            for (int iI = 0; iI < advisorCollection.Count; iI++)
	            {
	                comboBoxAdvisorCode.Items.Add(advisorCollection[iI].Code);
	            }
            }
            catch (System.Exception ex)
            {
            	
            }

            EnableControls(false);
        }

        private void comboBoxAdvisorCode_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void ClearFields()
        {
            textBoxAdvisorName.Text = "";
            textBoxTitle.Text = "";
            textBoxInitials.Text = "";
            textBoxSurname.Text = "";
            textBoxAddress1.Text = "";
            textBoxAddress2.Text = "";
            textBoxTelephone.Text = "";
            textBoxFax.Text = "";
        }

        private void comboBoxAdvisorCode_SelectionChangeCommitted(object sender, EventArgs e)
        {
            Advisor advisor = null;
            try
            {
                advisor = AdvisorServices.GetAdvisorByCode(comboBoxAdvisorCode.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Invalid advisor code  " + comboBoxAdvisorCode.Text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                EnableControls(false);
                bEditing = false;
                return;
            }

            if (advisor == null)
            {
                MessageBox.Show("Invalid advisor code  " + comboBoxAdvisorCode.Text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                EnableControls(false);
                bEditing = false;
                return;
            }

            EnableControls(true);

            textBoxAdvisorName.Text = advisor.Name;
            textBoxTitle.Text = advisor.Title;
            textBoxInitials.Text = advisor.Initials;
            textBoxSurname.Text = advisor.Surname;
            textBoxAddress1.Text = advisor.Address_1;
            textBoxAddress2.Text = advisor.Address_2;
            textBoxTelephone.Text = advisor.Telephone1;
            textBoxFax.Text = advisor.Fax;

            bEditing = true;
            sEditingAdvisor = comboBoxAdvisorCode.Text;
        }
    }
}
